/****************************************************
	文件：Logger.cs
	作者：HuskyT
	邮箱：1005240602@qq.com
	日期：2020/7/11 16:54:36
	功能：打印接口
*****************************************************/

using System;
using System.Collections.Generic;

namespace HTSpider
{
    public class Logger
    {
        private static Action<object> mOnLog;
        private static Action<object> mOnWarning;
        private static Action<object> mOnError;
        private static LogMemento mMemento;
        public static bool Activate { get; set; }

        public void OnDestroy()
        {
            mMemento.OnDestroy();
            mMemento = null;
        }
        public Logger(Action<object> onLog, Action<object> onWarning, Action<object> onError)
        {
            mOnLog = onLog;
            mOnWarning = onWarning;
            mOnError = onError;
            mMemento = new LogMemento(Consts.LOGMEMENTO_SAVE_PATH);
            mMemento.Init();
            Activate = true;
        }

        public static void Log(string msg)
        {
            if (Activate == false) return;
            mOnLog?.Invoke(msg);
            mMemento.Add(msg, LogMemento.LogMementoType.Log);
        }
        public static void LogFormat(string msg, params object[] args)
        {
            if (Activate == false) return;
            msg = string.Format(msg, args);
            mOnLog?.Invoke(msg);
            mMemento.Add(msg, LogMemento.LogMementoType.Log);
        }
        public static void Warning(string msg)
        {
            if (Activate == false) return;
            mOnWarning?.Invoke(msg);
            mMemento.Add(msg, LogMemento.LogMementoType.Warning);
        }
        public static void WarningFormat(string msg, params object[] args)
        {
            if (Activate == false) return;
            msg = string.Format(msg, args);
            mOnWarning?.Invoke(msg);
            mMemento.Add(msg, LogMemento.LogMementoType.Warning);
        }
        public static void Error(string msg)
        {
            if (Activate == false) return;
            mOnError?.Invoke(msg);
            mMemento.Add(msg, LogMemento.LogMementoType.Error);
        }
        public static void ErrorFormat(string msg, params object[] args)
        {
            if (Activate == false) return;
            msg = string.Format(msg, args);
            mOnError?.Invoke(msg);
            mMemento.Add(msg, LogMemento.LogMementoType.Error);
        }
    }
}
